package com.heny.demo.strategy;

import com.heny.demo.common.CommonErrorCode;
import com.heny.demo.common.CommonException;
import com.heny.demo.entity.TurnPointPOJO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author litao.cc
 * @version TxtProcessor.java v0.1 t2025/4/24 1:34
 * @date 2025/4/24 1:34
 * @description TxtProcessor
 */
public class TxtProcessor implements FileProcessorStrategy {
    private static final Logger logger = LoggerFactory.getLogger(TxtProcessor.class);
    @Override
    public List<TurnPointPOJO> processFile(MultipartFile file) {
        logger.info("TxtProcessor 开始处理文件：{0}",file.getName());
        List<TurnPointPOJO> result = new ArrayList<>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
            String line;
            TurnPointPOJO row;
            while ((line = reader.readLine()) != null) {
                row = new TurnPointPOJO();
                String[] values = line.split("\t");
                row = FileProcessorStrategy.buildPOJO(values);
                if (row != null){
                    result.add(row);
                }
            }
        } catch (IOException e) {
            logger.error("TxtProcessor 文件处理失败：{0}",e.getMessage());
            throw new CommonException(CommonErrorCode.FILE_SYS_ERROR,e.getMessage());
        }
        return result;
    }
}